package com.dw.controller;

import com.alibaba.fastjson.JSON;
import com.dw.entity.*;
import com.dw.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 测评控制器
 * @Author dw
 * @Date 2020/1/4 15:52
 * @Version 1.0
 **/
@Controller
@RequestMapping("/vote")
public class VoteController {

    /**
     * 返回到前端的数据集合
     **/
    Map<String,Object> resultMap;

    /**
     * 数据列名集合
     **/
    List<String> columnList;


    /**
     * 实体属性集合
     **/
    List<String> propertyList;

    /**
     * 条件集合
     **/
    Map<String,String> paramMap;

    /**
     * 用户服务层接口
     **/
    @Autowired
    private UserServiceImpl userService;

    /**
     问题服务
     */
    @Autowired
    private QuestionServiceImpl questionService;

    /**
     问题类型服务
     */
    @Autowired
    private QueTypeServiceImpl queTypeService;

    /**
     选项服务
     */
    @Autowired
    private ChoiceServiceImpl choiceService;

    /**
     答案服务
     */
    @Autowired
    private AnswerServiceImpl answerService;

    /**
     部门服务
     */
    @Autowired
    private DepServiceImpl depService;

    /**
     * 跳转内容显示页面
     *
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/goAnswerList")
    public String goAnswerList(){
        return "answer_list";
    }

    /**
     * 跳转经理内容显示页面
     *
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/goAnswerListJL")
    public String goAnswerListJL(){
        return "answer_list_jl";
    }


    /**
     * 跳转发布测评页面
     *
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/goAddQuestion")
    public String goAddQuestion(){
        return "addQuestion";
    }

    /**
     * 加载所有答案信息
     *
     *
     *
     * @param createTime
     * @param limit
     *
     * @param page
     * @return
     */
    @RequestMapping("/loadAnswerList")
    @ResponseBody
    public Object loadAnswerList(String createTime, Integer page, Integer limit,String queId,String voteBy,String check){
        paramMap = new HashMap<>();
        paramMap.put("createTime",createTime);
        paramMap.put("queId",queId);
        paramMap.put("voteBy",voteBy);
        paramMap.put("check",check);
        List<Map<Object,Object>> objects = answerService.getPageList("answer",(page-1)*limit,limit,paramMap);
        for (int i = 0; i < objects.size(); i++) {
            try {

                //查询问题
                Map<String,String> queParamMap = new HashMap<>();
                queParamMap.put("columnOne","queId");
                queParamMap.put("propertyOne",objects.get(i).get("queId").toString());
                Question question = questionService.findByOne("question",queParamMap);
                //添加createUser,并且将数据返回到前端页面
                objects.get(i).put("question",question);

                //查询发布者
                Map<String,String> createUserParamMap = new HashMap<>();
                createUserParamMap.put("columnOne","userId");
                createUserParamMap.put("propertyOne",objects.get(i).get("createBy").toString());
               User createUser = userService.findByOne("user",createUserParamMap);
                //添加createUser,并且将数据返回到前端页面
                objects.get(i).put("createUser",createUser);

                //查询回答者
                Map<String,String> answerUserParamMap = new HashMap<>();
                answerUserParamMap.put("columnOne","userId");
                answerUserParamMap.put("propertyOne",objects.get(i).get("answerBy").toString());
                User answerUser = userService.findByOne("user",answerUserParamMap);
                //添加answerUser,并且将数据返回到前端页面
                objects.get(i).put("answerUser",answerUser);

                //查询测评人
                Map<String,String> voteParamMap = new HashMap<>();
                voteParamMap.put("columnOne","userId");
                voteParamMap.put("propertyOne",objects.get(i).get("voteBy").toString());
                User voteUser = userService.findByOne("user",answerUserParamMap);
                //添加answerUser,并且将数据返回到前端页面
                if(voteUser != null){
                    objects.get(i).put("voteUser",voteUser);
                }


                //查询测评部门
                Map<String,String> depParamMap = new HashMap<>();
                depParamMap.put("columnOne","depId");
                depParamMap.put("propertyOne",objects.get(i).get("voteBy").toString());
               Department voteDepartment = depService.findByOne("department",depParamMap);
                //添加answerUser,并且将数据返回到前端页面
                if(voteDepartment != null){
                    objects.get(i).put("voteDepartment",voteDepartment);
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Integer count = answerService.getTotalCount("answer");
        Map<String,Object> res = new HashMap<>();
        res.put("code",0);
        res.put("msg","");
        res.put("count",count);
        res.put("data",objects);
        return JSON.toJSON(res);
    }


    /**
     加载所有问题类型
     */
    @RequestMapping("/loadAllQuestType")
    @ResponseBody
    public Object loadAllQuestType(){
        paramMap = new HashMap<>();
        resultMap = new HashMap<>();
        List<QueType> queTypeList = queTypeService.findAll("quetype",paramMap);
        resultMap.put("queTypeList",queTypeList);
        return JSON.toJSON(queTypeList);
    }

    /**
     * 加载其他类型的测评题目信息
     *
     * @param
     * @return
     */
    @RequestMapping("/loadAllQuestion")
    @ResponseBody
    public Object loadAllQuestion(String voteBy){
        System.out.println(voteBy+"voteBy");
        paramMap = new HashMap<>();
        resultMap = new HashMap<>();
        paramMap.put("column","voteBy");
        paramMap.put("property",voteBy);
        List<Question> questionList = questionService.findAll("question",paramMap);
        resultMap.put("questionList",questionList);

        //查询题目对应的选项信息
       List<List<Choice>> choiceListAll = new ArrayList<>();
        for(int i = 0; i < questionList.size();i++){
            Question que = questionList.get(i);
            Map<String,String> choiceParamMap = new HashMap<>();
            choiceParamMap.put("column","queId");
            choiceParamMap.put("property",que.getQueId().toString());
            List<Choice> choiceList = new ArrayList<>();
            choiceList = choiceService.findAll("choice",choiceParamMap);
            choiceListAll.add(choiceList);
        }

        resultMap.put("choiceListAll",choiceListAll);
        return JSON.toJSON(resultMap);
    }
    /**
     * 发布测评
     *
     * @param question
     * @param count  选项个数
     * @return
     */
    @RequestMapping("/addQuestion")
    @ResponseBody
    public Object addQuestion(Question question,String count,String choice0,String choice1,String choice2,String choice3,String choice4,String choice5){
        resultMap = new HashMap<>();
        columnList = new ArrayList<>();
        propertyList = new ArrayList<>();
        //插入问题
        columnList.add("queType");
        columnList.add("queTitle");
        columnList.add("voteBy");
        propertyList.add(question.getQueType().toString());
        propertyList.add(question.getQueTitle());
        propertyList.add(question.getVoteBy());
        questionService.insert("question",columnList,propertyList);

        //获取插入新问题的id
        paramMap = new HashMap<>();
        paramMap.put("last","queId");
       Question currentQue = questionService.findByOne("question",paramMap);
        //插入问题所对应的选项
        for (int i = 0; i < Integer.parseInt(count); i++) {
            columnList = new ArrayList<>();
            propertyList = new ArrayList<>();
            //选项所属的问题
            columnList.add("queId");
            //选项内容
            columnList.add("choContent");
            propertyList.add(currentQue.getQueId().toString());
           switch (i){
               case 0:
                   propertyList.add(choice0);
                   break;
               case 1:
                   propertyList.add(choice1);
                   break;
               case 2:
                   propertyList.add(choice2);
                   break;
               case 3:
                   propertyList.add(choice3);
                   break;
               case 4:
                   propertyList.add(choice4);
                   break;
               case 5:
                   propertyList.add(choice5);
                   break;
           }
           choiceService.insert("choice",columnList,propertyList);
            //获取插入选项的id
            paramMap = new HashMap<>();
            paramMap.put("last","choId");
            Choice currentCho =choiceService.findByOne("choice",paramMap);

            //修改问题所对应的选项
            columnList = new ArrayList<>();
            propertyList = new ArrayList<>();
            Map<String,String> idMap = new HashMap<>();
            resultMap = new HashMap<>();
            columnList.add("choId");

            propertyList.add(currentCho.getChoId().toString());;

            idMap.put("column","queId");
            idMap.put("property",currentQue.getQueId().toString());

           questionService.update("question",columnList,propertyList,idMap);
           resultMap.put("msg","ok");

        }

        return JSON.toJSON(resultMap);
    }
}
